package com.lwq.mall.seckill.interceptor;

import com.msb.common.constant.AuthConstant;
import com.msb.common.vo.MemberVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 秒杀活动的拦截器 自定义的拦截器 获取用户信息
 * 通过session共享获取
 */
public class AuthInterceptor implements HandlerInterceptor {

    //本地线程对象 Map<String,Object>
    public static ThreadLocal<MemberVO> threadLocal = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //通过httpsession获取用户信息
        HttpSession session = request.getSession(false);
        Object attribute = session.getAttribute(AuthConstant.AUTH_SESSION_REDIS);
        if(attribute != null){
            MemberVO memberVO = (MemberVO) attribute;
            threadLocal.set(memberVO);
            return true;
        }
        //如果attribute = null 说明没有登录 需要重定向到登录页面
        request.getSession().setAttribute(AuthConstant.AUTH_SESSION_MSG,"请先登录");
        response.sendRedirect("http://localhost:10010/login.html");
        return false;
    }
}
